package com.h3c.nem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.custom.BusinessException;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.annotation.CheckRole;
import com.h3c.nem.dto.OptionsDTO;
import com.h3c.nem.dto.SdDeptDTO;
import com.h3c.nem.entity.SdDept;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.service.ISdDeptService;
import com.h3c.nem.service.ISdNoticeRecordService;
import com.h3c.nem.utils.ReturnCodeData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: SdDeptController
 *
 * @Author: l22886
 * @Date: 2022-01-19
 * Description: sd_dept controller
 * Version: 1.0
 */
@Api(tags = " SdDept 接口")
@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/sdDept",produces = "application/json;charset=utf-8")
public class SdDeptController {

    @Resource
    private ISdDeptService sdDeptService;

    /**
     * @title: queryFuzzyListForPage
     * @description: 列表分页模糊查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "SdDeptDTO", paramType = "query"),
        @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> queryFuzzyListForPage(@ModelAttribute SdDeptDTO paramsDTO,
                                                            @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //分页
            Page page = PageHelper.startPage(currentPage, pageSize);
            //执行
            List<SdDeptDTO> resultDTOList = sdDeptService.selectFuzzyByDto(paramsDTO);
            PageInfo<SdDeptDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);
            data.put("data",pageInfo);
            data.put("msg", "列表分页模糊查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行列表分页模糊查询异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * @title: queryListForPage
     * @description: 列表分页精确查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "SdDeptDTO", paramType = "query"),
    @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
    @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryListForPage", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> queryListForPage(@ModelAttribute SdDeptDTO paramsDTO,
                                                       @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                       @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //分页
            Page page = PageHelper.startPage(currentPage, pageSize);
            //执行
            List<SdDeptDTO> resultDTOList = sdDeptService.selectFuzzyByDto(paramsDTO);
            PageInfo<SdDeptDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);
            data.put("data",pageInfo);
            data.put("msg", "列表分页查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行列表分页查询异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行列表分页查询完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }


    /**
     * @title: save
     * @description: 数据插入
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "SdDept", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> save(@RequestBody SdDeptDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //执行
            Integer result = sdDeptService.insertDto(paramsDTO);
            data.put("data",result);
            data.put("msg", "数据插入成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行 数据插入 异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * @title: update
     * @description: 数据修改
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "SdDept", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> update(@RequestBody SdDeptDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //执行
            Integer result = sdDeptService.updateDto(paramsDTO);
            data.put("data",result);
            data.put("msg", "数据修改成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行 数据修改 异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * @title: delete
     * @description: 数据删除
     * @params: [ id ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "SdDept", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> delete(@RequestBody SdDeptDTO paramsDTO) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //执行
            Integer result = sdDeptService.deleteDto(paramsDTO);
            data.put("data",result);
            data.put("msg", "数据删除成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行 数据删除 异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", "数据删除失败");
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    @CheckRole
    @RequestMapping(value = "/synceSdDept", method = RequestMethod.POST)
    public Result synceSdDept(@RequestBody SdDept sdDept) {
        sdDeptService.synceSdDept(false);
        return new Result(true, 20010, "同步成功");
    }


    @ApiOperation(value = "查询树选择项", notes = "查询树选择项")
    @RequestMapping(value = "/queryDeptTreeSelection", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> queryDeptTreeSelection(String deptIds) {
        log.info("准备执行 查询树选择项");
        JSONObject data = new JSONObject();
        try {
            //执行
            List<OptionsDTO> result = sdDeptService.queryDeptTreeSelection(deptIds);
            data.put("data", result);
            data.put("msg", "查询树选择项成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行 查询树选择项 异常，异常信息为：", e);
            data.put("msg", "查询树选择项失败");
            data.put("code", ReturnCodeData.FAIL);
        }
        //log.info("执行 查询树选择项 完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    @CheckRole
    @RequestMapping(value = "/qyeryChidrenDeptList", method = RequestMethod.POST)
    public Result qyeryChidrenDeptList(@RequestBody SdDeptDTO sdDeptDTO) {
        List<SdDeptDTO> sdDeptDTOS = sdDeptService.qyeryChidrenDeptList(sdDeptDTO);
        return new Result(true, StatusCodeEnum.SEARCH_SUCCESS, sdDeptDTOS);

    }

    @CheckRole
    @RequestMapping(value = "/syncDeptManager", method = RequestMethod.POST)
    public Result syncDeptManager() {
        sdDeptService.syncDeptManager();
        return new Result(true, StatusCodeEnum.NORMAL);

    }

    @CheckRole
    @RequestMapping(value = "/setDeptUserRole", method = RequestMethod.POST)
    public Result setDeptUserRole(@RequestBody SdDeptDTO sdDeptDTO) {
        Integer integer=sdDeptService.setDeptUserRole(sdDeptDTO);
        return new Result(true, StatusCodeEnum.NORMAL, integer);

    }

    @CheckRole
    @ApiOperation(value = "部门信息导出", notes = "部门信息导出")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "SdUserDTO", paramType = "body")})
    @RequestMapping(value = "/downloadDeptData", method = RequestMethod.POST)
    public Result downloadData(@RequestBody SdDeptDTO paramsDTO) {
        try {
            File downLoadFile = sdDeptService.download(paramsDTO,"EXPORT_DEPT_LIST","部门信息导出");
            FileInputStream inputStream = new FileInputStream(downLoadFile);
            byte[] buffer = new byte[(int) downLoadFile.length()];
            inputStream.read(buffer);
            inputStream.close();
            Map<String, String> result = new HashMap<>();
            result.put("file", Base64Utils.encodeToString(buffer));
            result.put("filename", downLoadFile.getName());
            return new Result(true, StatusCodeEnum.NORMAL,result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StatusCodeEnum.valueOf(""));
        }
    }

    @CheckRole
    @ApiOperation(value = "部门人员信息导入", notes = "部门人员信息导入")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "导入数据文件", required = true, dataType = "MultipartFile", paramType = "body")})
    @RequestMapping(value = "/importDeptSet", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> importDeptSet(@RequestParam(value="file") MultipartFile file) {
        Integer result = sdDeptService.importDeptSetList(file);
        JSONObject data = new JSONObject();
        data.put("data", result);
        data.put("msg", "部门人员导入");
        data.put("code", ReturnCodeData.SUCCESS);

        log.info("执行 部门人员导入完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * @title:
     * @description:
     * @createTime: 2022-03-31
     * @version: 1.0
     * @author: j26296
     */
    @ApiOperation(value = "部门邮件", notes = "部门邮件")
    @RequestMapping(value = "/sendEmailForDept", method = RequestMethod.POST)
    public Result sendEmailForDept() {
        try {
            sdDeptService.sendEmailForDept();
            return new Result(true, StatusCodeEnum.NORMAL, "success");

        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StatusCodeEnum.valueOf(""));
        }
    }



    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public Result modify() {
        sdDeptService.modify();
        return new Result(true, StatusCodeEnum.NORMAL);

    }

    @Resource
    private ISdNoticeRecordService iSdNoticeRecordService;

    @RequestMapping(value = "/sapCancel", method = RequestMethod.POST)
    public Result sapCancel(@RequestParam(value="workerId") String workerId) {
        TbOfferWorker offerWorker = new TbOfferWorker();
        offerWorker.setWorkerId(workerId);
        iSdNoticeRecordService.sapOfferWorkerCancellation(offerWorker);
        return new Result(true, StatusCodeEnum.NORMAL);

    }
}
